/*
 * Copyright (c) 2006, 2023, webrx.cn All rights reserved.
 *
 */
package cn.webrx.servlet;

import javax.servlet.ServletException;
import javax.servlet.annotation.MultipartConfig;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.Part;
import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * <p></p>
 * <p>Powered by webrx On 2023-03-24 09:40:23</p>
 *
 * @author webrx [webrx@126.com]
 * @version 1.0
 * @since 17
 */
@WebServlet("/save")
@MultipartConfig
public class Save extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        System.out.println("doGet()");
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("utf-8");
        resp.setCharacterEncoding("utf-8");
        resp.setContentType("text/html;charset=utf-8");
        PrintWriter out = resp.getWriter();
        out.println("<!DOCTYPE html>");
        out.println("<html>");
        out.println("<head>");
        out.println("<meta charset=\"utf-8\">");
        out.println("<title>玉灵 QQ:7031633 Email:webrx@126.com</title>");
        out.println("<meta name=\"keywords\" content=\"关键字\">");
        out.println("<meta name=\"description\" content=\"简介\">");
        out.println("</head>");
        out.println("<body>");

        System.out.println(req.getParameter("username"));
        System.out.println(req.getParameter("password"));
        System.out.println(req.getParameter("gender"));
        //Part username = req.getPart("username");
        //System.out.println(username.getContentType());
        //System.out.println(username.getName());
        //System.out.println(username.getSize());
        //System.out.println(username.getSubmittedFileName());
        //System.out.println(username.getInputStream());

        //处理上传单文件
        Part fs = req.getPart("fs");
        String path = String.format("user/upload/%tY%<tm%<td/", System.currentTimeMillis());
        File file = new File(req.getServletContext().getRealPath(path));
        if (!file.exists()) file.mkdirs();

        String filename = fs.getSubmittedFileName();
        Set<String> exts = new HashSet<>();
        exts.addAll(List.of("jpg", "gif", "png", "rar", "zip"));
        if (filename.lastIndexOf(".") != -1 && exts.contains(filename.substring(filename.lastIndexOf(".") + 1))) {
            fs.write(file.getAbsolutePath() + "/" + filename);
            out.print("上传成功"+file.getAbsolutePath() + "/" + filename);
        } else {
            out.print("上传失败，请检查文件格式。");
        }
        out.println("</body>");
        out.println("</html>");
        out.flush();
        out.close();
    }


}
